package com.springBoot.springBootSysCore.modules.controller.web.system;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.springBoot.springBootSysCore.modules.controller.web.base.BaseController;
import com.springBoot.springBootSysCore.modules.entity.system.SysOrganization;
import com.springBoot.springBootSysCore.modules.enums.base.DeleteStateEnum;
import com.springBoot.springBootSysCore.modules.services.system.SysOrganizationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("${adminPath}/organization")
public class SysOrganizationController extends BaseController<SysOrganization, String> {

	@Autowired
	private SysOrganizationService sysOrganizationService;


	@ModelAttribute("entity")
	public SysOrganization get(@RequestParam(required = false) String id) {
		if (StringUtils.isNotBlank(id)) {
			return sysOrganizationService.findById(id);
		} else {
			return new SysOrganization();
		}
	}
	/**
	 * 跳转到管理页面
	 * @param pageNumber
	 * @param pageSize
	 * @param model
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping("queryByPage")
	@ResponseBody
	public Object queryByPage(
            @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            Model model, HttpServletRequest request){
//		super.queryByPage(pageNumber,pageSize,model, request,"createDate","desc");
//		return "system/organization/organizationList";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("EQ_delFlag", DeleteStateEnum.NO_DELETE.getVal());
		return super.queryByPageForDataTables(model, request,map, "createDate", "desc");
	}
	/**
	 * 跳转到保存机构页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("findOne")
	public String findOne(@RequestParam(required=false)String id, Model model){
		return "system/organization/organizationChange";
	}

	/**
	 * 保存机构
	 * @param baseSysOrganization
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping("saveEntity")
	@ResponseBody
	public  Object saveEntity(SysOrganization baseSysOrganization) throws JsonProcessingException {
		return sysOrganizationService.saveEntity(baseSysOrganization);
	}

	/**
	 * 删除机构
	 * @param organizationIds
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping("deleteEntity")
	@ResponseBody
	public Object deleteEntity(@RequestParam(value="organizationIds[]",required=true)String[] organizationIds) throws JsonProcessingException {
		return sysOrganizationService.deleteEntity(Arrays.asList(organizationIds));
	}

	/**
	 * @return
	 * @throws JsonProcessingException
	 */
	@RequestMapping("getOrganizationListDateOfOrg")
	@ResponseBody
	public Object getOrganizationListDateOfOrg(String organizationId) throws JsonProcessingException {
		return sysOrganizationService.findTreeRes(organizationId);
	}
}
